package com.zretc.auth.service;

import com.zretc.api.pojo.SysUser;
import com.zretc.api.pojo.User;

import com.zretc.auth.dao.UserMapper;
import com.zretc.common.constant.SecurityConstant;
import com.zretc.common.constant.WebConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.common.util.JwtUtil;
import com.zretc.redis.service.RedisService;
import com.zretc.security.entity.LoginUser;
import org.bouncycastle.crypto.generators.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    @Autowired
    RedisService redisService;


    public RespResult login(User user){
        User srcUser = userMapper.getUserByName(user.getUsername());

        //验证用户名
        if(Objects.isNull(srcUser)){
            return R.fail(WebConstant.LOGIN_ERROR_CODE,WebConstant.ERROR_408);
        }
        //验证密码
        if(!new BCryptPasswordEncoder().matches(user.getPassword(),srcUser.getPassword())){

            return R.fail(WebConstant.LOGIN_ERROR_CODE,WebConstant.ERROR_408);
        }
        //如果登录成功- 直接查询权限列表
        ArrayList<String> roleList = userMapper.getRoleList(srcUser.getUserId());//system:dept:list
        //封装权限模型
        LoginUser loginUser = new LoginUser(srcUser, roleList,null );
        //根据userId生成JWT字符串
        String token = JwtUtil.createJWT(String.valueOf(srcUser.getUserId()));
        //存储登录信息到redis
        redisService.setCacheObject("login:" + srcUser.getUserId(),loginUser);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put(SecurityConstant.AUTHENTICATION_HEADER_NAME,token);
        return R.success(resultMap);

    }
    public User getUserById(Integer userId){
        System.out.println("UserService-----------------------------------------------------------------------"); // test
        User user = userMapper.getUserById(userId);
        System.out.println(user);
        return user;
    }

    public User getUserByUsername(String userName){
        User user = userMapper.getUserByName(userName);
        System.out.println(user);
        return user;
    }



//    public RespResult changePointOrRepu(User user) {
//        if (Objects.isNull(user)) {
//            return R.fail(WebConstant.no_ERROR_CODE, WebConstant.ERROR_301);
//        } else {
//            userMapper.updatePointOrRepu(user);
//            return R.success("操作成功");
//        }
//    }
//
//    public RespResult changeNicknameOrPhone(User user, User newInfo) {
//        if(Objects.isNull(user))
//        {
//            return R.fail(404, WebConstant.ERROR_404);
//        }
//        else if(/*Objects.isNull(newInfo.getPassword()) || */Objects.isNull(newInfo.getNickName()))
//        {
//            return R.fail(310, "请输入必要信息" );
//        }
//        else
//        {
//            user.setNickName(newInfo.getNickName());
//            user.setPhoneNumber(newInfo.getPhoneNumber());
//            userMapper.updateNicknameOrPhone(user);
//            return R.success("操作成功");
//        }
//    }
}
